﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DTO;
using DAO;

namespace BUS
{
    public class SanPham_BUS
    {
        private static SanPham_BUS m_instance = null;

        public static SanPham_BUS Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new SanPham_BUS();
                return m_instance;
            }
        }

        /// <summary>
        /// Lấy toàn bộ danh sách Sản Phẩm
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<SanPham_DTO> LayToanBoSanPham()
        {
            List<SanPham_DTO> result = new List<SanPham_DTO>();

            DataTable dt = SanPham_DAO.Instance.LayToanBoSanPham();
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return result;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = list[i];
                string masp = (string)row["MaSanPham"];
                string tensp = (string)row["TenSanPham"];
                string maloaisp = (string)row["MaLoaiSanPham"];
                //long dongia = (long)row["DonGia"];
                decimal dongia2 = (decimal)row["DonGia"];
                long dongia = (long)dongia2;
                int sl = (int)row["SoLuongTon"];
                string tenloaisp = (string)row["TenLoaiSanPham"];
                SanPham_DTO sp = new SanPham_DTO(masp, tensp, maloaisp, tenloaisp, dongia, sl);
                result.Add(sp);
            }
            return result;
        }

        /// <summary>
        /// Lấy toàn bộ Mã Sản Phẩm
        /// </summary>
        /// <returns></returns>
        public List<string> LayToanBoMaSanPham()
        {
            List<string> result = new List<string>();

            DataTable dt = SanPham_DAO.Instance.LayToanBoMaSanPham();
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return result;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = list[i];
                string masp = (string)row["MaSanPham"];
                result.Add(masp);
            }
            return result;
        }

        /// <summary>
        ///   tìm kiếm sản phẩm
        /// </summary>
        /// 
        public List<SanPham_DTO> TimKiemSanPham(SanPham_DTO _item)
        {
            List<SanPham_DTO> result = new List<SanPham_DTO>();
            DataTable dt = SanPham_DAO.Instance.TimKiemSanPham(_item.MaSanPham, _item.TenSanPham, _item.LoaiSP.TenLoaiSanPham, _item.DonGia.ToString(), _item.SoLuongTon.ToString());
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return result;
            }
            for (int i = 0; i < list.Count;i++ )
            {
                DataRow dr = list[i];
                string masp = (string)dr["MaSanPham"];
                string tensp = (string)dr["TenSanPham"];
                string maloaisp = (string)dr["MaLoaiSanPham"];
                decimal dongia2 = (decimal)dr["DonGia"];
                long dongia = (long)dongia2;
                int sl = (int)dr["SoLuongTon"];
                string tenloaisp = (string)dr["TenLoaiSanPham"];
                SanPham_DTO sp = new SanPham_DTO(masp, tensp, maloaisp, tenloaisp, dongia, sl);
                result.Add(sp);
            }
            return result;
        }

        /// <summary>
        /// Xóa sản phẩm
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        public bool XoaSanPham(SanPham_DTO sp)
        {
            return SanPham_DAO.Instance.XoaSanPham(sp.MaSanPham);
        }

        /// <summary>
        /// Cập nhật sản phẩm
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        public bool CapNhatSanPham(SanPham_DTO sp)
        {
            return SanPham_DAO.Instance.CapNhatSanPham(sp.MaSanPham, sp.TenSanPham, sp.LoaiSP.MaLoaiSanPham, sp.DonGia, sp.SoLuongTon);
        }

        /// <summary>
        /// Thêm sản phẩm mới
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        public bool ThemSanPham(SanPham_DTO sp)
        {
            return SanPham_DAO.Instance.ThemSanPhamMoi(sp.TenSanPham, sp.LoaiSP.MaLoaiSanPham, sp.DonGia, sp.SoLuongTon);
        }

        /// <summary>
        /// Lấy thông tin sản phẩm
        /// </summary>
        /// <param name="masp"></param>
        /// <returns></returns>
        public SanPham_DTO LayThongTinSanPham(string masp)
        {
            DataTable dt = SanPham_DAO.Instance.LayThongTinSanPham(masp);
            // Bi loi
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return null;

            DataRow row = list[0];
            string ma = (string)row["MaSanPham"];
            string tensp = (string)row["TenSanPham"];
            string maloai = (string)row["MaLoaiSanPham"];
            string tenloai = (string)row["TenLoaiSanPham"];
            decimal dongia2 = (decimal)row["DonGia"];
            long dongia = (long)dongia2;
            int sl = (int)row["SoLuongTon"];

            return new SanPham_DTO(ma, tensp, maloai, tenloai, dongia, sl);
        }   

    }
}
